package com.jizhangben.controller;

import com.jizhangben.pojo.AccountPojo;
import com.jizhangben.pojo.UserPojo;
import com.jizhangben.service.AccountService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet("/account")
public class AccountServlet extends HttpServlet {
    private AccountService accountService = new AccountService();
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Object userPojo = session.getAttribute("user");
        UserPojo user = (UserPojo) userPojo;
        req.setAttribute("yourName", user.getName());
        // 控制层调用业务逻辑层获取数据
        List<AccountPojo> accountPojos = accountService.getByAll(user.getId());
        // 转发到jsp中
        req.setAttribute("accounts", accountPojos);
        req.getRequestDispatcher("views/account.jsp").forward(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("views/account.jsp").forward(req, resp);
    }
}
